package com.augurit.gzcgwsp.sendNotice.web;

import java.util.List;
import java.util.Map;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;
import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.base.extmodel.ExtResult;
import com.augurit.common.base.extmodel.base.ITreeNode;
import com.augurit.common.base.form.ResultForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.sendNotice.convert.YwZhengwuInfoConvert;
import com.augurit.gzcgwsp.sendNotice.entity.YwZhengwuInfo;
import com.augurit.gzcgwsp.sendNotice.service.IYwZhengwuInfoService;
import com.augurit.gzcgwsp.sendNotice.web.form.YwZhengwuInfoForm;
import com.augurit.gzcgwsp.state.util.RequestUtil;

@SuppressWarnings("serial")
@Results( {
		@Result(name = CrudActionSupport.RELOAD, location = "zhengwu-info.action", type = "redirect"),
		@Result(name = "toAllList", location = "/gzcgwsp/state/listAllZhenwuInfo.jsp"),
		@Result(name = "toList", location = "/gzcgwsp/state/listZhenwuInfo.jsp"),
		@Result(name = "000", location = "/gzcgwsp/state/ywZhengwuInfo/000.jsp"),
		@Result(name = "001", location = "/gzcgwsp/state/ywZhengwuInfo/001.jsp"),
		@Result(name = "002", location = "/gzcgwsp/state/ywZhengwuInfo/002.jsp"),
		@Result(name = "003", location = "/gzcgwsp/state/ywZhengwuInfo/003.jsp"),
		@Result(name = "004", location = "/gzcgwsp/state/ywZhengwuInfo/004.jsp"),
		@Result(name = "005", location = "/gzcgwsp/state/ywZhengwuInfo/005.jsp"),
		@Result(name = "006", location = "/gzcgwsp/state/ywZhengwuInfo/006.jsp"),
		@Result(name = "007", location = "/gzcgwsp/state/ywZhengwuInfo/007.jsp"),
		@Result(name = "008", location = "/gzcgwsp/state/ywZhengwuInfo/008.jsp"),
		@Result(name = "009", location = "/gzcgwsp/state/ywZhengwuInfo/009.jsp"),
		@Result(name = "010", location = "/gzcgwsp/state/ywZhengwuInfo/010.jsp"),
		@Result(name = "011", location = "/gzcgwsp/state/ywZhengwuInfo/011.jsp") })
public class ZhengwuInfoAction extends CrudActionSupport<YwZhengwuInfoForm> {

	private YwZhengwuInfoForm form;
	private Long id;
	private Page<YwZhengwuInfoForm> page;
	@Autowired
	private IYwZhengwuInfoService ywZhengwuInfoService;

	public String wfBusSave() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String deleteMore() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		String serialNo = getRequest().getParameter("serialNo");
		System.out.println("serialNo000 = "+serialNo);
		getRequest().setAttribute("serialNo", serialNo);
		return "toList";
	}
	
	public String allList() throws Exception {

		return "toAllList";
	}

	/**
	 * 查询数据列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getAllData() throws Exception {
		Map map = RequestUtil.getMapByRequest(getRequest());
		map.put("sphjdm", "001");
		if (page == null) {
			page = new Page(15);
		}
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		page = ywZhengwuInfoService.searchPage(page, map);

		renderText(Struts2Utils.getJsonString(page,
				DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	/**
	 * 获取项目名称
	 */
	public void getXmmc(){
		List<BasicCombo> xmmcComboList = ywZhengwuInfoService.getXmmc();
		this.extRenderListJson(xmmcComboList, true);
	}
	
	/**
	 * 获取环节名称
	 */
	public void getSphjdmmc(){
		List<BasicCombo> sphjdmmcComboList = ywZhengwuInfoService.getSphjdmmc();
		this.extRenderListJson(sphjdmmcComboList, true);
	}

	/**
	 * 根据serialNo，获取该案件的所有交换记录
	 * @return
	 * @throws Exception
	 */
	public String getAllDataBySerialNo() throws Exception {
		// System.out.println("id ======================= " + id);
		String serialNo = getRequest().getParameter("serialNo");
		System.out.println("serialNo111 = "+serialNo);
		Map map = RequestUtil.getMapByRequest(getRequest());
		if (page == null) {
			page = new Page(15);
		}
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		page = ywZhengwuInfoService.searchPage(page, map);
		renderText(Struts2Utils.getJsonString(page,
				DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	public String info() throws Exception {
		// System.out.println("id ======================= " + id);
		String info = this.getParameter("sphjdm");

		if (id != null) {
			form = ywZhengwuInfoService.get(id);
		} else {
			form = new YwZhengwuInfoForm();
		}
		return info;
	}

	public String saveYwZhengwuInfo() throws Exception {
		try {
			String callTimes = this.getParameter("callTimes");
			YwZhengwuInfo entity = new YwZhengwuInfo();
			YwZhengwuInfoConvert.convertFormToVo(form, entity);
			ywZhengwuInfoService.save(entity);
			this.renderJson(new ResultForm(true));
		} catch (RuntimeException e) {
			this.renderJson(new ResultForm(false));
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (id != null) {
			form = ywZhengwuInfoService.get(id);
		} else {
			form = new YwZhengwuInfoForm();
		}
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public YwZhengwuInfoForm getModel() {
		return form;
	}

	public Page<YwZhengwuInfoForm> getPage() {
		return page;
	}

	public void setPage(Page<YwZhengwuInfoForm> page) {
		this.page = page;
	}

	public IYwZhengwuInfoService getYwZhengwuInfoService() {
		return ywZhengwuInfoService;
	}

	public void setYwZhengwuInfoService(
			IYwZhengwuInfoService ywZhengwuInfoService) {
		this.ywZhengwuInfoService = ywZhengwuInfoService;
	}

	public YwZhengwuInfoForm getForm() {
		return form;
	}

	public void setForm(YwZhengwuInfoForm form) {
		this.form = form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	/**
	 * 将对象列表转换为JSON字符串
	 * 注意：方法已经对树结构对象作去掉空列表处理
	 * @param list 对象列表
	 * @param autoCount 是否需要统计对象列表数量
	 */
	public static void extRenderListJson(final List list, boolean autoCount) {
		
		String jsonString = "";
		
		//JSON字符串转换
		if(autoCount){
			ExtResult result = new ExtResult(true, true);
			result.setResult(list);
			jsonString = getJsonString(result);
		}else{
			jsonString = getJsonString(list);
		}
		
		//输出JSON字符串。如果是树结构对象，则去除空children属性的JSON字符
		if(list != null && list.size() > 0 && list.get(0) instanceof ITreeNode)
			renderJson(ExtUtils.removeEmptyChildrenFromTreeNode(jsonString));
		else
			renderJson(jsonString);
	}
}
